package system

import (
	"context"
	"gf-assistant/internal/common/consts"
	"gf-assistant/internal/common/factory"
	"gf-assistant/internal/system/parameter"
	"github.com/gogf/gf/v2/util/gconv"
	"reflect"
)

type IndexService struct {
	indexDao string
}

func init() {
	indexService := IndexService{
		indexDao: "system.IndexDao",
	}
	factory.Put(reflect.TypeOf(indexService).String(), &indexService)
}
func (s *IndexService) GetMenu(ctx context.Context, req *parameter.GetMenuReq) (res *parameter.GetMenuRes) {
	req.SetUserId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	menuInfos := factory.Get(s.indexDao).(*IndexDao).GetMenu(ctx, req)
	menuInfos = s.getTreeRecursive(menuInfos, 0)
	return &parameter.GetMenuRes{
		HomeInfo_: parameter.HomeInfo{
			Title: "首页",
			Href:  "page/welcome-1.html?t=1",
		},
		LogoInfo_: parameter.LogoInfo{
			Title: "LAYUI MINI",
			Image: "images/logo.png",
			Href:  "",
		},
		MenuInfo: menuInfos,
	}
}
func (s *IndexService) getTreeRecursive(list []*parameter.MenuRes, parentId uint) []*parameter.MenuRes {
	res := make([]*parameter.MenuRes, 0)
	for _, v := range list {
		if v.ParentId == parentId {
			v.Children = s.getTreeRecursive(list, v.Id)
			res = append(res, v)
		}
	}
	return res
}
